package dao

import (
	"morleyStoneHome/model"
)

type catcoinDao struct {
}

var CatcoinDao = newcatcoinDao()

func newcatcoinDao() *catcoinDao {
	return &catcoinDao{}
}

//增单条
func (n *catcoinDao) Insert(catcoin *model.CatcoinDto) error {
	err := Db.Create(catcoin).Error
	if nil != err {
		return err
	}
	return nil
}

//更新单条
func (n *catcoinDao) Update(addr string, catcoin *model.Catcoin) error {
	err := Db.Model(&model.Catcoin{}).Where("addr = ? ", addr).Updates(catcoin).Error
	if nil != err {
		return err
	}
	return nil
}

//查单条
func (n *catcoinDao) FindOne(addr string) (cat []model.Catcoin, err error) {
	res := Db.Where("addr = ? ", addr).Find(&cat)
	if res.Error != nil {
		return nil, res.Error
	}
	return cat, nil
}

//根据邀请码查单条
func (n *catcoinDao) FindOneByInv(invCode int) (cat []model.Catcoin, err error) {
	res := Db.Where("inv_code = ? ", invCode).Find(&cat)
	if res.Error != nil {
		return nil, res.Error
	}
	return cat, nil
}

//根据质押状态查单条
func (n *catcoinDao) FindOneByPledged(pledged int) (cat []model.Catcoin, err error) {
	res := Db.Where("pledged = ? ", pledged).Find(&cat)
	if res.Error != nil {
		return nil, res.Error
	}
	return cat, nil
}

//根据uid,作为puid查他的所有直推
func (n *catcoinDao) FindFollowerList(puid int) (cat []model.Catcoin, err error) {
	res := Db.Where("puid = ? ", puid).Find(&cat)
	if res.Error != nil {
		return nil, res.Error
	}
	return cat, err
}

//找到所有做出市场的cat
func (n *catcoinDao) FindAllMarketerList() (cat []model.Catcoin, err error) {
	res := Db.Where("inv_count != 0").Find(&cat)
	if res.Error != nil {
		return nil, res.Error
	}
	return cat, err
}

//找到所有被推荐人的cat
func (n *catcoinDao) FindAllFollowerList() (cat []model.Catcoin, err error) {
	res := Db.Select("uid").Where("puid != 0").Find(&cat)
	if res.Error != nil {
		return nil, res.Error
	}
	return cat, err
}
